#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- This's client main frame window
# --
# ****************************
# Author: lmaye.Zhou
# Blog: www.lmay.com
# Date: 2017年7月11日 11:42:58
# ----------------------------------------------------------
import random

from PyQt5.QtCore import QBasicTimer
from PyQt5.QtGui import QIcon, QPalette, QColor
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QWidget, QProgressBar, QGridLayout, QLabel
from files.logs import Logger
from files.tools import ConfigUtils


class MainWindow(QMainWindow):
    _log = Logger.log();

    def __init__(self):
        self.step = 0;
        self.timer = QBasicTimer()
        self.__conf = ConfigUtils();
        # window abs path
        self.__iconPath = self.__conf.resourcesPath + self.__conf.themePath;
        # style relative path
        self.__iconRelPath = "./" + self.__conf.resources + self.__conf.themePath;
        self._log.info(self.__iconPath + " -- %s", self.__iconRelPath)
        super().__init__();

    def initQMainWindow(self):
        exitAction = QAction(QIcon(self.__iconPath + "/close.png"), "&Exit", self);
        exitAction.setShortcut("Ctrl+Q");
        exitAction.setStatusTip("Exit Application");
        exitAction.triggered.connect(qApp.quit);

        helpAction = QAction(QIcon(self.__iconPath + "/query.png"), "&Help", self);
        helpAction.setShortcut("Ctrl+H");
        helpAction.setStatusTip("QQ: 379839355");
        # helpAction.triggered.connect(qApp.quit);

        aboutAction = QAction(QIcon(self.__iconPath + "/contact.png"), "&About", self);
        aboutAction.setShortcut("Ctrl+A");
        aboutAction.setStatusTip("Author: lmay.Zhou [QQ: 379839355]");
        # aboutAction.triggered.connect(qApp.quit);

        # 状态栏工具条
        self.statusBar();
        menubar = self.menuBar();
        fileMenu = menubar.addMenu("&File");
        helpMenu = menubar.addMenu("&Help");
        # 事件绑定
        fileMenu.addAction(exitAction);
        helpMenu.addAction(helpAction);
        helpMenu.addAction(aboutAction);

        titleLabel = QLabel("客户端服务监听中 ... ");
        titleLabel.setStyleSheet(
            "QLabel{"
            "font-family: Arial;"
            "font-weight: bold;"
            "font-size: 15px;"
            "color: #104E8B;}");

        # 进度条
        self.aProgressBar = QProgressBar();
        self.aProgressBar.setValue(0);

        # 网格布局
        mainLayout = QGridLayout(self);
        # 添加布局
        mainLayout.addWidget(titleLabel, 0, 0, 1, 2);
        mainLayout.addWidget(self.aProgressBar, 1, 0, 3, 2);
        # 设置间距
        mainLayout.setSpacing(20);

        qWidget = QWidget();
        qWidget.setLayout(mainLayout);
        qWidget.setContentsMargins(10, 10, 10, 10)
        qPalette = QPalette();
        qPalette.setColor(QPalette.Background, QColor(213, 232, 251));
        # qPalette.setBrush(QPalette.Background, QBrush(QPixmap(iconPath + "/background.png")));
        qWidget.setAutoFillBackground(True);
        qWidget.setPalette(qPalette);
        self.setCentralWidget(qWidget);
        self.setWindowIcon(QIcon(self.__iconPath + "/favicon.png"));
        # 设置状态栏信息
        # self.setStatusTip("Author: lmay.Zhou [QQ: 379839355]");
        self.setFixedSize(350, 280);
        self.setWindowTitle("发票数据客户端 - 主程序");
        self.show();

    # 进度条控制
    def timerEvent(self, event):
        if self.step >= 100:
            self.step = 0;
            return;
        self.step += random.randint(0, 9);
        self.aProgressBar.setValue(self.step);
